package com.jiahua.admin.biz.action.inventory;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.jiahua.admin.biz.service.InventoryCategoryService;
import com.jiahua.admin.biz.service.InventoryItemService;
import com.jiahua.user.biz.model.InventoryCategory;
import com.jiahua.user.biz.model.InventoryItem;
import com.opensymphony.xwork2.ActionSupport;

@Result(type = "stream", params = { "contentType", "${contentType}", "contentLength", "${contentLength}",
		"contentDisposition", "${contentDisposition}", "inputStream", "${inputName}", "bufferSize", "${bufferSize}",
		"allowCaching", "${allowCaching}" })
public class InventoryItemListAction extends ActionSupport implements ServletRequestAware {

	Logger logger = Logger.getLogger(InventoryItemListAction.class.getClass());

	byte[] imageInByte = null;

	String imageId;

	private HttpServletRequest servletRequest;

	private static final long serialVersionUID = -5520856308160159222L;

	private InventoryItemService inventoryItemService;

	private InventoryCategoryService inventoryCategoryService;

	private List<InventoryItem> inventoryItemList;

	private List<InventoryCategory> inventoryCategoryList;

	public List<InventoryItem> getInventoryItemList() {
		return inventoryItemList;
	}

	public void setInventoryItemList(List<InventoryItem> inventoryItemList) {
		this.inventoryItemList = inventoryItemList;
	}

	public List<InventoryCategory> getInventoryCategoryList() {
		return inventoryCategoryList;
	}

	public void setInventoryCategoryList(List<InventoryCategory> inventoryCategoryList) {
		this.inventoryCategoryList = inventoryCategoryList;
	}

	public void setInventoryItemService(InventoryItemService inventoryItemService) {
		this.inventoryItemService = inventoryItemService;
	}

	public void setInventoryCategoryService(InventoryCategoryService inventoryCategoryService) {
		this.inventoryCategoryService = inventoryCategoryService;
	}

	@Override
	public String execute() throws Exception {

		inventoryCategoryList = categoryList();
		logger.debug(">>> InventoryItemListAction ---- execute(). ");
		inventoryItemList = inventoryItemService.listInventoryItem();
		logger.info("list.size() = " + inventoryItemList.size());
		for (InventoryItem inventoryItem : inventoryItemList) {

			System.out.println(inventoryItem.getImage().toString());
		}

		return super.execute();
	}

	public String getCustomContentType() {
		return "image/jpeg";
	}

	public String getCustomContentDisposition() {
		return "anyname.jpg";
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.servletRequest = request;
	}

	public byte[] getCustomImageInBytes() {
		return imageInByte;
	}

	public List<InventoryCategory> categoryList() {
		return inventoryCategoryService.listInventoryCategory();
	}

}
